﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Active.Core;
public partial class Media_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string regionId = Request.QueryString["regionId"];
            if (!string.IsNullOrEmpty(regionId))
            {
                ddlRegions.SelectedValue = regionId;   
            }

            //客户部不能看到返点
            if (!Media.CanEditObject())
            {
                this.MediasGridView.Columns[this.MediasGridView.Columns.Count - 1].Visible = 
                this.MediasGridView.Columns[this.MediasGridView.Columns.Count - 2].Visible = false;
            }
        }
    }

    protected void ddlRegions_SelectedIndexChanged(object sender, EventArgs e)
    {
        Response.Redirect(string.Format("~/Media/?regionId={0}", this.ddlRegions.SelectedValue), true);
    }

    protected void aspnetPager_PageChanged(object sender, EventArgs e)
    {
        MediasGridView.DataBind();
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        MediasGridView.DataBind();
    }

    #region Business Methods

    private MediaList GetMediaList()
    {
        //清除Session中的业务对象, 避免出现Session保存错误的业务对象
        if (Session["CurrentObj"] != null)
        {
            Session["CurrentObj"] = null;
        }
        int regionId = int.Parse(this.ddlRegions.SelectedValue);
        string searchName = txtSearchName.Text.Trim();
        int pageNo = aspnetPager.CurrentPageIndex;
        int pageSize = aspnetPager.PageSize;
        MediaList list = null;
        try
        {
            list = MediaList.GetMediaList(regionId, searchName, pageNo, pageSize);
            aspnetPager.RecordCount = list.TotalRecordsCount;
        }
        catch (System.Security.SecurityException)
        {
            Response.Redirect("~/");
        }
        return list;
    }

    #endregion

    #region MediasDataSource

    protected void MediasDataSource_SelectObject(object sender, Csla.Web.SelectObjectArgs e)
    {
        e.BusinessObject = GetMediaList();
    }

    #endregion

    #region RegionsCslaDataSource

    protected void RegionsCslaDataSource_SelectObject(object sender, Csla.Web.SelectObjectArgs e)
    {
        e.BusinessObject = Regions.GetRegions();
    }

    #endregion
}
